#![windows_subsystem = "windows"]
use std::time::Duration;

use chrono::Local;
use fltk::enums::{Color, Event, FrameType, Shortcut};
use fltk::prelude::WidgetExt;
use fltk::{app, frame::Frame, window};
use fltk::{button, dialog, frame, input, menu, prelude::*, text};
fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gtk);
    let mut window = window::Window::default()
        .with_size(800, 700)
        .with_label("Fltk window test");
    window.clone().center_screen();
    let mut menu_bar = menu::MenuBar::default().with_pos(0, 0).with_size(800, 20);
    menu_bar.set_frame(FrameType::FlatBox);
    menu_bar.set_color(Color::from_rgb(250, 250, 250));
    menu_bar.add(
        "文件/新建\t",
        Shortcut::Ctrl | 'n',
        menu::MenuFlag::Normal,
        println_menu,
    );
    menu_bar.add(
        "文件/退出\t",
        Shortcut::Ctrl | 'x',
        menu::MenuFlag::Normal,
        println_menu,
    );

    menu_bar.add(
        "帮助/更新\t",
        Shortcut::None,
        menu::MenuFlag::Normal,
        println_menu,
    );
    menu_bar.add(
        "帮助/关于\t",
        Shortcut::None,
        menu::MenuFlag::Normal,
        println_menu,
    );
    app::background(240, 240, 240);
    window.clone().make_resizable(true);
    let mut frame_btn = Frame::default()
        .with_pos(10, 60)
        .with_size(780, 60)
        .with_label("www");
    let input_word = input::Input::new(10, 70, 280, 30, "");
    let mut btn_start = button::Button::new(295, 70, 60, 30, "@< 传输");
    let btn_stop = button::Button::new(360, 70, 60, 30, "@>| 清除");
    let mut frame_textarea = Frame::new(10, 130, 780, 500, "textarea");
    let text_content = text::TextBuffer::default();
    let mut text_edit = text::TextEditor::default()
        .with_size(780, 400)
        .with_pos(10, 130);
    text_edit.set_buffer(text_content.clone());
    frame_textarea.set_frame(FrameType::EngravedBox);
    frame_textarea.set_color(Color::from_rgb(250, 250, 250));
    btn_start.set_shortcut(Shortcut::Ctrl | 'b' | 'B');
    frame_btn.set_frame(fltk::enums::FrameType::EngravedBox);
    let mut frame_status_bar = frame::Frame::new(10, 640, 780, 40, "");
    frame_status_bar.set_frame(FrameType::EngravedBox);
    let mut lable_status = button::Button::new(10, 650, 60, 20, "状态:");
    lable_status.set_frame(FrameType::NoBox);
    let mut lable_time = button::Button::new(640, 650, 140, 20, "");
    lable_time.set_frame(FrameType::NoBox);
    window.end();
    window.show();
    let mut btn_stop_clone = btn_stop.clone();
    let mut text_content_clone_2 = text_content.clone();
    btn_stop_clone.set_callback(move |_| {
        text_content_clone_2.set_text("");
    });
    let mut text_content_clone = text_content.clone();
    btn_start.clone().set_callback(move |_| {
        println!("{}", text_content.text());
        text_content_clone.append(&input_word.value());
        text_content_clone.append("\n");
    });
    let mut i = 0;
    window.set_callback(|_| {
        if app::event() == Event::Close {
            exit();
        }
    });
    time_update(lable_time.clone());

    while app.wait() {
        if text_edit.changed() {
            i += 1;
            println!("有变化{}", i);
            text_edit.clear_changed();
        }
    }
    app.run().unwrap();
}

fn println_menu(m: &mut impl MenuExt) {
    if let Some(f) = m.choice() {
        match f.as_str() {
            "新建\t" => println!("{}", f),
            "退出\t" => exit(),
            _ => println!("{}", f),
        }
    }
}

//退出
fn exit() {
    let (width, height) = app::screen_size();
    let dialog = dialog::choice2(
        (width / 2.0) as i32 - 150,
        (height / 2.0) as i32 - 150,
        "真的要退出吗?",
        "取消",
        "立即退出",
        "",
    )
    .unwrap();
    if dialog == 1 {
        app::quit();
    }
}

fn get_time() -> String {
    let fmt = "%Y-%m-%d %H:%M:%S";
    let now = Local::now();
    let dft = now.format(fmt);
    dft.to_string()
}

fn time_update(lable_time: button::Button) {
    std::thread::spawn(move || loop {
        let time = get_time();
        std::thread::sleep(Duration::from_millis(1000));
        lable_time.clone().with_label(&time);
    });
}
